df <- dget(here::here("DataCollection/PNAS analysis/Data", "dfFin.rda"))

# Load Packages -----------------------------------------------------------

library(wesanderson)
library(psych)
library(stargazer)
library(reshape)
library(ggplot2)
library(grid)
library(gridExtra)
library(knitr)
library(scales)
library(plyr)
library(rddtools)
library(quantreg)
library(GGally)
library(psych)
library(lmtest)
library(sandwich)


# cowplot -----------------------------------------------------------------

# For display item 3  I need a function from the package cowplot. As loading the
# package changes basic plotting options, I will directly access the function
# without loading the package. However, the package has to be installed.
if (!is.element("cowplot", installed.packages()[,1])) {
  stop("Please install the cowplot package")
}else{
  print("cowplot package is installed")  
  }

# Seed --------------------------------------------------------------------

set.seed(546992)

# Colours -----------------------------------------------------------------

# Colours
ColBlind <- c('#a6cee3','#1f78b4','#b2df8a','#33a02c')
Col2 <- c('#8dd3c7','#ffffb3','#bebada','#fb8072')
Col3 <- c('#66c2a5','#fc8d62','#8da0cb','#e78ac3')
Col4 <- c('#e41a1c','#377eb8','#4daf4a','#984ea3')

# Define IPL-12 variables and covariates ----------------------------------

source(here::here("DataCollection/PNAS analysis/Code", "dataPrep.r"))

# Sample descriptives -----------------------------------------------------

# N
table(df$survey)

# Age
round(by(df$age, df$survey, mean), digits = 2)
round(by(df$age, df$survey, sd), digits = 2)

# Residency in host country
round(by(df$yearsIn, df$survey, mean), digits = 2)
round(by(df$yearsIn, df$survey, sd), digits = 2)
round(by(df$yearsIn, df$survey, median), digits = 2)

# Household size
round(by(df$hhsize, df$survey, mean), digits = 2)
round(by(df$hhsize, df$survey, sd), digits = 2)

# Gender
round(by(df$gender, df$survey, mean), digits = 4)

# Sample means
round(by(df$ipl12s, df$survey, mean), digits = 2)

# Average residency
mean(df$yearsIn)


# Variable Descriptives ---------------------------------------------------

source(here::here("DataCollection/PNAS analysis/Code", "variableDescriptives.r"))


# Figure 1 ----------------------------------------------------------------

source(here::here("DataCollection/PNAS analysis/Code", "displayItem1.r"))

# Regressions -------------------------------------------------------------

source(here::here("DataCollection/PNAS analysis/Code", "regressionAnalysis.r"))


# Figure 2 left panel -----------------------------------------------------

source(here::here("DataCollection/PNAS analysis/Code", "coefPlots.r"))


# Figure 2 right panel ----------------------------------------------------

source(here::here("DataCollection/PNAS analysis/Code", "quantileReg.r"))


# Figure 2 ----------------------------------------------------------------

source(here::here("DataCollection/PNAS analysis/Code", "displayItem2.r"))


# Figure 3 ----------------------------------------------------------------

source(here::here("DataCollection/PNAS analysis/Code", "displayItem3.r"))


# Correlations ------------------------------------------------------------

source(here::here("DataCollection/PNAS analysis/Code", "correlations.r"))


# Cronbach's Alpha --------------------------------------------------------

source(here::here("DataCollection/PNAS analysis/Code", "cronbachsAlpha.r"))


# Survey time -------------------------------------------------------------

source(here::here("DataCollection/PNAS analysis/Code", "surveyTimeOut.r"))


# Robustness Display Item 1 -----------------------------------------------

source(here::here("DataCollection/PNAS analysis/Code", "RobustnessItem1.r"))


# PCA ---------------------------------------------------------------------

source(here::here("DataCollection/PNAS analysis/Code", "pca.r"))


# Instrument correlation --------------------------------------------------

source(here::here("DataCollection/PNAS analysis/Code", "ipl12_24cor.r"))


# Language pooled -------------------------------------------------------

source(here::here("DataCollection/PNAS analysis/Code", "languagePooled.r"))

